<?php


namespace app\api\controller\csmwj;

use addons\csmwj\library\xcore\xcore\base\XcAMyApi;
use addons\csmwj\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmwj\library\xcore\xcore\utils\XcRequestUtils;
use addons\csmwj\library\xcore\xcore\utils\XcUserSessionUtils;


class XcmyCloginApi extends XcAMyApi
{
    protected $noNeedMyRight = ["getSessionUserinfo", 'shenfenshenhe2'];

    public function xinit()
    {
    }

    public function getSessionUserinfo()
    {
        $userinfo = XcUserSessionUtils::getUserinfo(false);
        if (!empty($userinfo['province_id'])) {
            $userinfo['province_name'] = db('rui_province')->where('id', $userinfo['province_id'])->value('name');
        }

        if (!empty($userinfo['company_id'])) {
            $userinfo['company_name'] = db('rui_company')->where('id', $userinfo['company_id'])->value('name');
        }

        $this->success(null, ['userinfo' => $userinfo]);
    }

    public function updateUserinfo()
    {
        $userId = XcUserSessionUtils::getUserId();
        $nickname = XcRequestUtils::post("nickname");
        $avatar = XcRequestUtils::post("avatar");
        $user = XcDaoUtils::getRowById(new \app\admin\model\User(), $userId);
        $user->save([
            "nickname" => $nickname,
            "avatar" => $avatar
        ]);
        $this->success();
    }

    public function deleteUserinfo()
    {
        $userId = XcUserSessionUtils::getUserId();
        if (true) {
            $row = XcDaoUtils::getRowById(new \app\admin\model\User(), $userId);
            $row->save([
                'status' => 'hidden',
                'mobile' => '',
                'username' => $row->username . "-DEL"
            ]);
        }
        if (true) {
            $row = XcDaoUtils::getRowBySql(new \app\admin\model\csmwj\Cloginthird(), [
                ['user_id', '=', $userId]
            ], null, true, false);
            if ($row) {
                $row->save([
                    'status' => 'hidden'
                ]);
            }
        }

        $this->success();
    }

    //身份审核
    public function shenfenshenhe()
    {

        $userinfo = XcUserSessionUtils::getUserinfo(false);
        $userid = $userinfo['id'];
        $info = db('rui_shenfenshenhe')->where('user_id', $userid)->find();
        if ($info) {
            $this->error('您已提交过审核');
        }

        $data['user_id'] = $userid;
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['type'] = input('type');
        $data['status'] = 0;
        $data['memo'] = input('memo');
        $data['image'] = input('diangongzheng');
        $data['zhengjian_daoqi'] = input('zhengjian_daoqi');

        if (!empty(input('province_id'))) {
            $data['province_id'] = db('rui_province')->where('name', input('province_id'))->value('id');
        }

        if (!empty(input('company_id'))) {
            $data['company_id'] = db('rui_company')->where('name', input('company_id'))->value('id');
        }

        $insert = db('rui_shenfenshenhe')->insert($data);

        $this->success('成功提交申请，等待审核', '成功提交申请，等待审核');
    }

    //身份审核后套操作
    public function shenfenshenhe2()
    {
        $info = db('rui_shenfenshenhe')->where('id', input('id'))->find();
        $status = input('status');
        if ($status == 1) {
            db('rui_shenfenshenhe')->where('id', input('id'))->update(['status' => 1]);

            $data['shenfen'] = $info['type'];
            $data['province_id'] = $info['province_id'];
            $data['company_id'] = $info['company_id'];
            $data['zhengjian'] = $info['image'];
            $data['zhengjian_daoqi'] = $info['zhengjian_daoqi'];
            $nickname = '';
            if (!empty($data['province_id'])) {
                $nickname .= db('rui_province')->where('id', $data['province_id'])->value('name');
            }

            if (!empty($data['company_id'])) {
                $nickname .= db('rui_company')->where('id', $data['company_id'])->value('name');
            }
            $nickname .= $info['memo'];
            $data['nickname'] = $nickname;
            db('user')->where('id', $info['user_id'])->update($data);
        } else {
            db('rui_shenfenshenhe')->where('id', input('id'))->update(['status' => 2]);
        }


        $this->success('成功提交申请，等待审核', '成功提交申请，等待审核');
    }

    //预警提交
    public function yujingtijiao()
    {
        $userinfo = XcUserSessionUtils::getUserinfo(false);
        $userid = $userinfo['id'];

        $data['user_id'] = $userid;
        $data['area'] = input('area');
        $data['code'] = input('code');
        $data['image'] = input('image');

        $data['miaoshu'] = input('miaoshu');
        $data['name'] = input('name');
        $data['qingkuang'] = input('qingkuang');

        $data['yuanyin'] = input('yuanyin');


        $data['create_at'] = date('Y-m-d H:i:s');

        $insert = db('rui_yujing')->insert($data);
        //发送信息

        $this->success('提交成功', '提交成功');
    }

    //历史报警记录
    public function getYujingDetail()
    {
        $list = db('rui_yujing')->where('id', input('id'))->find();

        $this->success('提交成功', $list);
    }

    //历史报警记录
    public function yujingList()
    {
        if(input('type') == 1){
            $userinfo = XcUserSessionUtils::getUserinfo(false);
            $userid = $userinfo['id'];
            $list = db('rui_yujing')->where('user_id',$userid)->order('id desc')->select();
        }else{
            $list = db('rui_yujing')->order('id desc')->select();
        }

        foreach ($list as &$v) {
            $v['nickname'] = db('user')->where('id', $v['user_id'])->value('nickname');
        }
        $this->success('提交成功', $list);
    }

    //获取列表
    public function getList()
    {
        $list = db('csmwj_paper')->where('status', 'normal')->select();

        $this->success('成功', $list);
    }

    //获取列表 个人
    public function gettijiaoList()
    {
        $userinfo = XcUserSessionUtils::getUserinfo(false);
        $userid = $userinfo['id'];
        if(!empty(input('user_id'))){
            $userid = input('user_id');
        }
        $list = db('csmwj_userpaper')->order('id desc')
            ->where('status', 'normal')
            ->where('csmwj_weixinuser_id', $userid)
            ->where('answerstatus', '=', 'FINISH')
            ->select();

        foreach ($list as &$v) {
            $v['nickname'] = db('user')->where('id', $v['csmwj_weixinuser_id'])->value('nickname');
            $v['create_at'] = date('Y-m-d H:i:s', $v['createtime']);
            $v['title'] = db('csmwj_paper')->where('id', $v['csmwj_paper_id'])->value('name');
        }
        $this->success('成功', $list);
    }

    //获取列表 按人
    public function gettijiaoListAll()
    {
        $list = db('csmwj_userpaper')->order('id desc')
            ->where('status', 'normal')
            ->where('csmwj_weixinuser_id', input('uid'))
            ->where('answerstatus', '=', 'FINISH')
            ->select();
        foreach ($list as &$v) {
            $v['nickname'] = db('user')->where('id', $v['csmwj_weixinuser_id'])->value('nickname');
            $v['create_at'] = date('Y-m-d H:i:s', $v['createtime']);
            $v['title'] = db('csmwj_paper')->where('id', $v['csmwj_paper_id'])->value('name');
        }
        $this->success('成功', $list);
    }

    //获取列表 员工 所有
    public function getbaobiaouser()
    {
        $a = db('csmwj_userpaper')->order('id desc')->where('status', 'normal')->where('answerstatus', '=', 'FINISH')->column('csmwj_weixinuser_id');
        $userList = db('user')->where('id', 'in', $a)->select();
        foreach ($userList as &$v) {
            $count = db('csmwj_userpaper')->where('csmwj_weixinuser_id', $v['id'])->where('status', 'normal')->where('answerstatus', '=', 'FINISH')->count();
            $v['tijiao'] = '提交次数:' . $count . '次';
        }
        $this->success('成功', $userList);
    }

    public function getXiala()
    {
        $a = db('rui_company')->select();
        $b = db('rui_province')->select();
        $data['a'] = array_column($a, 'name');
        $data['b'] = array_column($b, 'name');
        $this->success(null, $data);
    }

    public function getZhengjian()
    {
        if (input('type') == '2') {
            //未过期
            $list = db('user')->where('status', 'normal')
                ->where('zhengjian_daoqi', '<=', date('Y-m-d H:i:s'))
                ->order('zhengjian_daoqi asc')->select();
        } elseif (input('type') == '3') {

            //已过期
            $list = db('user')->where('status', 'normal')
                ->where('zhengjian_daoqi', '>', date('Y-m-d H:i:s'))
                ->order('zhengjian_daoqi asc')->select();
        } else {
            //全部
            $list = db('user')->where('status', 'normal')
                ->where('zhengjian_daoqi', '>', 0)
                ->order('zhengjian_daoqi asc')->select();
        }

        foreach ($list as &$v) {
            if ($v['zhengjian_daoqi'] >= date('Y-m-d H:i:s')) {
                $v['ttp'] = '未到期';
            } else {
                $v['ttp'] = '已到期';
            }

            $v['company_name'] = db('rui_company')->where('id', $v['company_id'])->find()['name'];
        }
        $this->success(null, $list);
    }
}
